package com.leyou.search.web;

import com.leyou.search.dto.SearchParamDTO;
import com.leyou.search.entity.Goods;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.dto.PageInfo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("goods")
public class SearchController {
    @Resource
    private SearchService searchService;

    //创建索引库并导入数据
    @GetMapping("init")
    public ResponseEntity<String> init() {
        searchService.createIndexAndMapping();
        searchService.loadData();
        return ResponseEntity.ok("索引库已初始化并导入数据成功");
    }

    //查询自动补全数据
    @GetMapping("/suggestion")
    public Mono<List<String>> getSuggestion(@RequestParam("key") String key) {
        return searchService.getSuggestion(key);
    }

    /**
     * 分页搜索商品数据
     */
    @PostMapping("/list")
    public Mono<PageInfo<Goods>> searchGoods(@RequestBody SearchParamDTO searchParamDTO) {
        return searchService.searchGoods(searchParamDTO);
    }
}

